import grideItem from "./gridItem";

export default class IGridView {

    parent: Laya.Sprite;

    /**格子开始位置 */
    x: number = 0;

    y: number = 20;

    //**格子尺寸 */
    height: number = 100;

    width: number = 100;

    /**格子间距 */
    spaceX: number = 0;

    spaceY: number = 0;

    /**格子数量 */
    countX: number = 5;

    countY: number = 35;
}


export class gridView {

    private readonly mouseEventFun = { [Laya.Event.MOUSE_DOWN]: "onMouseDown", [Laya.Event.MOUSE_UP]: "onMouseUp", [Laya.Event.MOUSE_OUT]: "onMouseOut", }

    private itemList: grideItem[] = [];

    private grideActive: boolean;


    constructor(private gridInfo?: IGridView) {
        this.gridInfo = this.gridInfo || new IGridView();

        for (let i = 0; i < this.gridInfo.countX; i++) {
            for (let j = 0; j < this.gridInfo.countY; j++) {

                // let cellIndex = 
            }
        }
    }

    updateGrid() {

    }

    /**格子是否激活 */
    setActive(active: boolean) {
        if (this.grideActive === active) return;
        this.grideActive = active;

        this.activateMouseEvent(active);
        this.updateGrid();
    }


    private onMouseDown(e: Laya.Event) {
    }


    private onMouseUp(e: Laya.Event) {
    }


    private onMouseOut(e: Laya.Event) {

    }


    private activateMouseEvent(active: boolean, ...type: string[]) {
        let p = this.gridInfo.parent;
        let fun = active ? p.on.bind(this) : p.off.bind(this);
        let funkey = type ? type : Object.keys(this.mouseEventFun);
        type.forEach(v => p.offAll(v));
        if (!active) return;

        funkey.forEach(v => fun(v, this, this[this.mouseEventFun[v]]));
    }


} 